using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Emlyn.Website.Articles;
using System.ComponentModel;
using Emlyn.Website.Base;

namespace Emlyn.Website.ArticlesPresentation
{
    public class PresentArticleList: PresentBase
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                return Convert.ToString(ViewState["Title"]);
            }
            set
            {
                ViewState["Title"] = Convert.ToString(value);
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public IList<Article> aArticleList
        {
            get
            {
                IList<Article> retval = new List<Article>();

                string lArticleCodes = Convert.ToString(ViewState["ArticleCodes"]);
                if (!String.IsNullOrEmpty(lArticleCodes))
                {
                    string[] lArticleCodeArr = lArticleCodes.Split(',');
                    ArticleQuery laq = new ArticleQuery();

                    foreach (string lArticleCode in lArticleCodeArr)
                    {
                        Article lArticle = laq.GetArticleByArticleCode(lArticleCode);
                        retval.Add(lArticle);
                    }
                }

                return retval;
            }
            set
            {
                string lArticleCodes = String.Empty;

                foreach (Article larticle in value)
                {
                    if (!String.IsNullOrEmpty(lArticleCodes))
                        lArticleCodes += ",";
                    lArticleCodes += larticle.ArticleCode;
                }

                ViewState["ArticleCodes"] = lArticleCodes;
            }
        }

        IPresentArticleFactory _presentArticleFactory;

        public IPresentArticleFactory aPresentArticleFactory
        {
            get
            {
                return _presentArticleFactory;
            }
            set
            {
                _presentArticleFactory = value;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Panel lbasePanel = new Panel();
            lbasePanel.CssClass = CssClass + "ArticleList";
            Controls.Add(lbasePanel);

            if (!String.IsNullOrEmpty(Title))
            {
                Literal llit;
                Panel lpnl;

                lpnl = new Panel();
                lpnl.CssClass = CssClass + "ArticleListTitle";
                lbasePanel.Controls.Add(lpnl);

                llit = new Literal();
                llit.Text = Title;
                lpnl.Controls.Add(llit);

                lbasePanel.Controls.Add(lpnl);
            }

            foreach (Article larticle in aArticleList)
            {
                aPresentArticleFactory.aCSSPrefix = CssClass;
                aPresentArticleFactory.aEditable = Editable;

                PresentArticle lPresentArticle = aPresentArticleFactory.ConstructPresentArticle();
                lPresentArticle.aArticle = larticle;

                lbasePanel.Controls.Add(lPresentArticle);
            }
        }
    }
}
